//
//  StatusPictureView.swift
//  weibo
//
//  Created by mac on 15/12/19.
//  Copyright © 2015年 rong. All rights reserved.
//

import UIKit

private let pictureCellMargin: CGFloat = 5
private let pictureCellID = "pictureCellID"
class StatusPictureView: UICollectionView {
    //定义外部模型属性
    var imageURLs: [NSURL]? {
        didSet {
            //修改配图大小
            let pSize = caclePictureViewSize()
            //更新配图视图的大小
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(pSize)
            }
            //修改测试的文案
            //testLable.text = "\(imageURLs?.count ?? 0)"
        //刷新列表
            reloadData()
        }
    }
    //重写构造方法
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        //实例化一个流水布局
        let flowLayout = UICollectionViewFlowLayout()
        //设置间距
        flowLayout.minimumInteritemSpacing = pictureCellMargin
        flowLayout.minimumLineSpacing = pictureCellMargin
        super.init(frame: frame, collectionViewLayout: flowLayout)
        backgroundColor = UIColor.whiteColor()
        //注册cell
        self.registerClass(pictureCell.self, forCellWithReuseIdentifier: pictureCellID)
        //设置数据源 代理
        self.dataSource = self
        //设置视图
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //计算配图的大小
    private func caclePictureViewSize() ->CGSize {
        //计算配图大小
        //获取配图的数量
        let imageCount = imageURLs?.count ?? 0
        //获取配图视图的最大宽度
        let maxWidth = screenW - 2 * StatusCellMarigin
        let itemWidth = (maxWidth - 2 * pictureCellMargin) / 3
        //取出collectionView的流水布局
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        //设置itemsize
        layout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        //没有图片
        if imageCount == 0 {
            return CGSizeZero
        }
        //单张图片
        if imageCount == 1 {
            //单图会按照图片等比例显示
            //先给定尺寸
            let imageSize = CGSize(width: 180, height: 120)
            return imageSize
        }
        if imageCount == 4 {
            //多图如果是4张会按照2*2显示
            let w = itemWidth * 2 + pictureCellMargin
            return CGSize(width: w, height: w)
        }
        //如果程序走到这里 就是其他的多张图片
        //确定有多少行
        let row = CGFloat((imageCount - 1) / 3 + 1)
        return CGSize(width: maxWidth, height: row * itemWidth + (row - 1) * pictureCellMargin)
    }
    //设置页面 和布局
    private func setupUI() {
//        self.addSubview(testLable)
//        testLable.snp_makeConstraints { (make) -> Void in
//            make.center.equalTo(self.center)
        }
    }
    //懒加载所有的子视图
    //private lazy var testLable: UILabel = UILabel(title: "", color: UIColor.redColor(), fontSize: 30)
//}
//数据源方法
extension StatusPictureView: UICollectionViewDataSource {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageURLs?.count ?? 0
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        //使用这个方法 一定要注册cell
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(pictureCellID, forIndexPath: indexPath) as! pictureCell
        cell.imageURL = imageURLs![indexPath.item]
        return cell
    }
}
class pictureCell: UICollectionViewCell {
    //定于外部模型属性
    var imageURL: NSURL? {
        didSet {
            iconView.sd_setImageWithURL(imageURL)
        }
    }
    //重写构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //设置页面 和布局
    private func setupUI() {
        contentView.addSubview(iconView)
        //设置布局
        iconView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(snp_edges)
        }
    }
    //懒加载所有的子视图
    private lazy var iconView: UIImageView = {
        let iv = UIImageView()
        iv.contentMode = UIViewContentMode.ScaleAspectFill
        //在手码开发中 图片的默认的裁剪 是没有开启的 需要手动裁剪
        iv.clipsToBounds = true
        return iv
    }()
}
